package com.shujia.wyh.day26;

public class SetThread2 implements Runnable {
    private Student2 student;
    private int i = 0;
//    private Object obj = new Object();

    private Demo demo;

    SetThread2(Student2 student) {
        this.student = student;
    }

    SetThread2(Student2 student,Demo demo) {
        this.student = student;
        this.demo = demo;
    }

    @Override
    public void run() {
        while (true){
            synchronized (demo){
                //先看看学生对象有没有值
                //如果这里flag值是false,说明学生对象的成员变量是没有值，或者说这里的值还没有被消费，就不等待消费者
                //直接生产数据
                //如果这里是true,说明学生对象有数据，生产者等待消费者消费数据
                if(student.flag){
                    //调用wait方法等待
                    //共有的锁对象调用方法
                    try {
//                        student.wait();
                        demo.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                if(i%2==0){
                    student.name = "李毅";
                    student.age = 18;
                }else {
                    student.name = "小虎";
                    student.age = 16;
                }

                i++;
                //生产完数据后，通知消费者来消费数据
//                student.notify();
                demo.notify();
                student.flag = true;
            }

        }


    }
}
